﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ShowInfoPanel:MonoBehaviour
{
    private Button btn_discrible;
    private Button btn_skill;
    private Button btn_attribute;
    private Button btn_close;

    /// <summary>
    /// 管理角色信息的四个面板
    /// </summary>
    private List<IInfoPanel> infoPanelLis = new List<IInfoPanel>();
    private string currentRoleName;//当前要显示的角色名称
    public int currentIndex { get; set; }
    private int TargetIndex; 
    public int targetIndex
    {
        get { return TargetIndex; }
        private set { TargetIndex = value; }
    }
    private IInfoPanel headInfoPanel;
    private IInfoPanel lastInfoPanel;

    public void Init()
    {
        btn_discrible = FindObj.FindChild(transform, "btn_discrible").GetComponent<Button>();
        btn_skill = FindObj.FindChild(transform, "btn_skill").GetComponent<Button>();
        btn_attribute = FindObj.FindChild(transform, "btn_attribute").GetComponent<Button>();
        btn_close = FindObj.FindChild(transform, "btn_close").GetComponent<Button>();

        currentIndex = targetIndex = 0;

        btn_discrible.onClick.AddListener(() =>
        {
            targetIndex = 0;
            PlayScrollAnim();
        });
        btn_skill.onClick.AddListener(() =>
        {
            targetIndex = 1;
            PlayScrollAnim();
        });
        btn_attribute.onClick.AddListener(() =>
        {
            targetIndex = 2;
            PlayScrollAnim();
        });
        btn_close.onClick.AddListener(Hide);
        InitInfoPanel();
    }

    public void Show(string roleName)
    {
        gameObject.SetActive(true);
        currentRoleName = roleName;
        for (int i = 0; i < infoPanelLis.Count; i++)
        {
            infoPanelLis[i].SetInfo(roleName);
        }
    }
    public void Hide()
    {
        currentRoleName = null;
        for (int i = 0; i < infoPanelLis.Count; i++)
        {
            infoPanelLis[i].ClearInfo();
        }
        targetIndex = 0;
        lastInfoPanel.ResetScroll();
        ResetNote();
        gameObject.SetActive(false);
    }
    /// <summary>
    /// 重置List重每个panel的节点之间的关系回到默认状态
    /// </summary>
    void ResetNote()
    {
        IInfoPanel tempPanel;
        int upper = infoPanelLis.Count;
        for (int i = 0; i < upper; i++)
        {
            int preIndex = i - 1;
            int nxtIndex = i + 1;
            tempPanel = infoPanelLis[i];
            if (preIndex >= 0)
                tempPanel.prePanel = infoPanelLis[preIndex];
            if (nxtIndex < upper)
                tempPanel.nxtPanel = infoPanelLis[nxtIndex];
        }
    }

    /// <summary>
    /// 初始化角色信息的四个面板
    /// </summary>
    private void InitInfoPanel()
    {
        Transform infoPanelParent = transform.Find("panels/Mask").transform;
        IInfoPanel infoPanel = null;
        for (int i = 0; i < infoPanelParent.childCount; i++)
        {
            infoPanel = infoPanelParent.GetChild(i).GetComponent<IInfoPanel>();
            infoPanel.Init(this);//初始化面板
            infoPanelLis.Add(infoPanel);
        }
        headInfoPanel = infoPanelLis[0];
        lastInfoPanel = infoPanelLis[infoPanelLis.Count - 1];
        //获取前后
        IInfoPanel tempPanel;
        int upper = infoPanelLis.Count;
        for (int i = 0; i < upper; i++)
        {
            int preIndex = i - 1;
            int nxtIndex = i + 1;
            tempPanel = infoPanelLis[i];
            if (preIndex >= 0)
                tempPanel.prePanel = infoPanelLis[preIndex];
            if (nxtIndex < upper)
                tempPanel.nxtPanel = infoPanelLis[nxtIndex];
        }
    }
    /// <summary>
    /// 播放infoPanel的滑动动画
    /// </summary>
    private void PlayScrollAnim()
    {                 
        if (currentIndex - targetIndex <= 0)
        {//向上滑动
            headInfoPanel.ScrollNxt(); 
        }
        else//向下滑动
        {
            lastInfoPanel.ScrollPre();
        }
    }
}